﻿
Ext.namespace('FantastPlatform', 'FantastPlatform.UI');


FantastPlatform.UI.MaintainStabilityForm = function (config) {
    Ext.apply(this, config);
    Ext.override(Ext.form.CheckboxGroup, {
        //在inputValue中找到定义的内容后，设置到items里的各个checkbox中    
        setValue: function (value) {
            this.items.each(function (f) {
                if (value.indexOf(f.inputValue) != -1) {
                    f.setValue(true);
                } else {
                    f.setValue(false);
                }

            });
        },
        //以value1,value2的形式拼接group内的值   
        getValue: function () {
            var re = "";
            this.items.each(function (f) {
                if (f.inputValue != 'versa') {
                    if (f.getValue() == true) {

                        re += f.inputValue + ",";
                    }
                }
            });
            return re.substr(0, re.length - 1);
        },
        //在Field类中定义的getName方法不符合CheckBoxGroup中默认的定义，因此需要重写该方法使其可以被BasicForm找到   
        getName: function () {
            return this.name;
        }
    });
    this.leaderStr = 0;
    var count = leaderStore.getCount();
    var leaderCheckboxItems = [];
    for (var i = 0; i < count; i++) {
        var boxLabel = leaderStore.getAt(i).get("Name");
        var name = leaderStore.getAt(i).get("Id");
        var value = leaderStore.getAt(i).get("Code");
        leaderCheckboxItems.push({
            boxLabel: boxLabel,
            name: 'leader' + name,
            value: value,
            inputValue: value,
            width: 100
        });
    }
    leaderCheckboxItems.push({
        boxLabel: '反选',
        name: 'leaderVersa',
        value: 'versa',
        inputValue: 'versa',
        width: 50,
        listeners: { 'check': function (checkbox, checked) {
            leaderCheckboxGroup.items.each(function (f) {
                if (f.inputValue != 'versa') {
                    f.setValue(!f.getValue());
                }
            })
        }
        }
    });
    var leaderCheckboxGroup = new Ext.form.CheckboxGroup({
        fieldLabel: '<font color="red">*</font>&nbsp;&nbsp;上报领导',
        name: 'Leaders',
        hiddenName: 'Leaders',
        displayField: 'Name',
        mode: 'remote',
        valueField: 'Id',
        allowBlank: false,
        editable: false,
        triggerAction: 'all',
        blankText: '请选择上报领导',
        columns: 3,
        items: leaderCheckboxItems

    });

    this.items = [{
        layout: 'column',
        border: false,
        labelSeparator: '：',
        items: [{
            columnWidth: 1,
            border: false,
            layout: 'form',
            items: [new Ext.form.Hidden({
                name: 'CreatePoliceManNumber'
            })]
        },
{
    columnWidth: 1,
    border: false,
    layout: 'form',
    items: [new Ext.form.TextField({
        fieldLabel: '名称',
        name: 'Title',
        allowBlank: false,
        blankText: " 名称不能为空",
        width: 360
    })]
},  {
                    columnWidth: 1,
                    border: false,
                    layout: 'form',
                    items: [new Ext.form.TextArea({
                        fieldLabel: '详情',
                        name: 'Content',
                        allowBlank: false,
                        blankText: " 详情不能为空",
                        height: 150,
                        width: 360
                    })]
                }, {
                    columnWidth: 1,
                    border: false,
                    layout: 'form',
                    items: [leaderCheckboxGroup]
                }, {
                    columnWidth: 1,
                    border: false,
                    layout: 'form',
                    items: [new Ext.form.Checkbox({
                        fieldLabel: '<font color="red">是否发送手机短息</font>',
                        name: 'IsSendMsg',
                        allowBlank: true
                    })]
                }]
    }];

    FantastPlatform.UI.MaintainStabilityForm.superclass.constructor.call(this, {
        bodyStyle: 'padding:5px',
        collapsible: true,
        labelAlign: 'right',
        autoScroll: true,
        border: false
    });
};

Ext.extend(FantastPlatform.UI.MaintainStabilityForm, Ext.form.FormPanel, {
    onRender: function (ct, position) {
        FantastPlatform.UI.MaintainStabilityForm.superclass.onRender.call(this, ct, position);
        if (this.Id && this.Id != 0) {
            this.add(new Ext.form.Hidden({ name: 'Id', value: this.Id }));
            var Form = this.form;
            this.form.load({
                url: '../Service/MaintainStabilityViewService.aspx?Action=Get',
                params: { id: this.Id },
                waitMsg: '正在读取数据...',
                waitTitle: '请稍后...',
                failure: function (form, action) {
                    if (action.failureType != 'client') {
                        showErrorMessage('操作失败', action.result.message, '');
                    }
                },
                success: function (form, action) {

//                    if (form.getValues().CreatePoliceManNumber != policemanNumber && AdminI != "True") {
//                        Ext.getCmp('saveNotice').setDisabled(true);
//                        Ext.getCmp('cancelNotice').setDisabled(true);
//                        //                        Ext.getCmp('addDetail').setDisabled(true);
//                        //                        Ext.getCmp('ChangeCar').setDisabled(true);
//                    }
                }
            });
            Ext.Ajax.request({
                method: 'post',
                url: "../Service/MaintainStabilityViewService.aspx?Action=GetLeadersByMaintaStabilityId",
                params: { id: this.Id },
                success: function (request, options) {
                    // 服务器返回json形式的结果                                          
                    var reText = request.responseText;
                    var reTextObj = Ext.util.JSON.decode(reText);
                    if (eval(reTextObj.success)) {
                        Form.findField('Leaders').setValue(reTextObj.message);
                    }
                }
            });
        }
    }
});

